#include "stdafx.h"
#include "httpdbcommunicator.h"
#include "db.h"

httpdbcommunicator::httpdbcommunicator(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi( this );
    QObject::connect( ui.pushButton, SIGNAL( clicked() ), this, SLOT( onRequest() ) );
}

httpdbcommunicator::~httpdbcommunicator()
{

}

void httpdbcommunicator::onRequest()
{
    // select
 //   Db::DbReader dbReader;
 //   Db::DbString dbString = Db::DbString( "SELECT id, name, mail, phonenumber, somedate FROM players WHERE name = %1 OR 1=1" ).argValue( "martin" );
 //   if (! Db::select( dbString, dbReader ))
 //       return;

 //   while (dbReader.read())
 //   {
 //       int id = dbReader.getInt( 0 );
 //       QString name = dbReader.getString( 1 );
 //       QString mail = dbReader.getNvlString( 2, "zadaj mail" );
 //       QString phoneNumber = dbReader.getNvlString( 3 );
 //       QDate date = dbReader.getNvlDate( 4, QDate() );
 //   }

    // insert
    QFile file("in.txt");
	file.open(QIODevice::ReadOnly | QIODevice::Text);
	QTextStream in( &file );
    QString name = in.readAll();

	Db::DbString dbString = 
        Db::DbString( "INSERT INTO players (name, mail, phonenumber, somedate) VALUES( %1, %2, %3, %4 )" )
        .argValue( name )
        .argValue( "martinovemail@centrum.sk" )
        .argValue( "026241598687" )
        .argValue( QDate( 2010, 10, 14 ) );

    if (! Db::execute( dbString ))
        return;
}